clear
clc

% Generating the bit r(t) with each bit 6 samples long
b=round(rand(1,2));
r=[];
for k=1:2
    if b(1,k)==0
        sig=-ones(1,7);
    else
        sig=ones(1,7);
    end
    r=[r sig];
end
figure(101),plot(r);
axis([-1 20 -1.5 1.5]);
title('\bf\it Original Bit Sequence');

% Generating the pseudo random bit pattern for spreading
spread_sig=round(rand(1,14));
spread_sig_copy=spread_sig;
for i=1:14
    if spread_sig(1,i)==0
        spread_sig(1,i)=-1;
    end
end
% figure(102),plot(spread_sig);
% axis([-1 130 -1.5 1.5]);
% title('\bf\it Pseudorandom Bit Sequence');

% XORing the r(t) with the spread signal
hopped_sig=myxor(r,spread_sig);

% Modulating the hopped signal by bpsk technique
dsss_sig=[];
t=0:100;    
fc=.1;
c1=cos(2*pi*fc*t);
c2=cos(2*pi*fc*t+pi);
for k=1:14
    if hopped_sig(1,k)==-1
        dsss_sig=[dsss_sig c1];
    else
        dsss_sig=[dsss_sig c2];
    end
end

figure(103),plot(1:1414,dsss_sig);
axis([-1 1414 -1.5 1.5]);
title('\bf\it DSSS Signal');

%corrupt dsss signal with a sinusoid with frequency w0 =pi/4
%corrupted signal at the receiver
received_signal = dsss_sig + 10*sin(pi/4*(1:length(dsss_sig)));
figure(104),plot(1:1414,received_signal);
axis([-1 1414 -1.5 1.5]);
title('\bf\it Received Signal');


%corrupt dsss signal with white noise
% dsss_sig_corrupted_2 = awgn(dsss_sig,10,'measured');
% figure(105),plot(1:12120,dsss_sig_corrupted_2);
% axis([-1 12220 -1.5 1.5]);
% title('\bf\it corrupted Signal');

% % Plotting the FFT of DSSS signal
% figure(105),plot(1:12120,abs(fft(dsss_sig)));
% title('\bf\it fft Signal');